/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import DTO.DataType;
import java.awt.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author TrongKhoa
 */
public class MyEditTablePanel extends JPanel
{

    private BorderLayout _mainBorderLayout;
    private JPanel _centerPanel;
    private JPanel _rightPanel;
    private JTable _tblColumns;
    private JButton _btnSaveData;
    private JButton _btnAddSinhVien;
    private JButton _btnAddReferenceKey;
    private JButton _btnDeleteSinhVien;
    private JButton _btnUpdateSinhVien;
    private GridBagLayout _gridbagLayout;
    private GridBagConstraints _gridConstraints;
    private int _selectedRowIdx;
    private frmMain _parent;
    private JTextField _txtTableName;

    public MyEditTablePanel(frmMain parent)
    {
        super();
        _parent = parent;
        _mainBorderLayout = new BorderLayout(12, 12);
        setLayout(_mainBorderLayout);

        _centerPanel = new JPanel(new BorderLayout());
        initializeCenterPanel();

        add(_centerPanel, BorderLayout.CENTER);
        //_mainBorderLayout.addLayoutComponent(_centerPanel, BorderLayout.CENTER);

        _rightPanel = new JPanel();
        _gridbagLayout = new GridBagLayout();
        _rightPanel.setLayout(_gridbagLayout);
        initializeRightPanel();
        add(_rightPanel, BorderLayout.EAST);
    }

    public void SetTableModel(TableDesignModeValues model)
    {
        _tblColumns.setModel(model);
        _tblColumns.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        _txtTableName.setText(model._table.getTableName());

        JComboBox cbo = new JComboBox();
        cbo.addItem(DataType.INT);
        cbo.addItem(DataType.NVARCHAR);
        cbo.addItem(DataType.DATE);
        cbo.addItem(DataType.FLOAT);
        _tblColumns.setDefaultEditor(DataType.class, new DefaultCellEditor(cbo));
    }
    /*
     * public static void main(String[] args) { JFrame frm = new JFrame();
     * frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setSize(new
     * Dimension(800, 400)); frm.getContentPane().add(new
     * MyEditTablePanel(null)); frm.setVisible(true); }
     *
     */

    private void initializeCenterPanel()
    {
        _centerPanel.setLayout(new BorderLayout());
        JPanel panel1 = new JPanel();
        panel1.setLayout(new FlowLayout(FlowLayout.LEFT));
        _centerPanel.add(panel1, BorderLayout.NORTH);
        JLabel lblTableName = new JLabel("Table name:");

        panel1.add(lblTableName);

        _txtTableName = new JTextField(14);
        _txtTableName.setText("Table1");
        panel1.add(_txtTableName);

        _tblColumns = new JTable();
        Vector vt1 = new Vector();
        _tblColumns.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        ListSelectionModel rowSM = _tblColumns.getSelectionModel();
        rowSM.addListSelectionListener(new ListSelectionListener()
        {

            @Override
            public void valueChanged(ListSelectionEvent e)
            {
                /*
                 * //Ignore extra messages. if (e.getValueIsAdjusting()) {
                 * return; }
                 *
                 * ListSelectionModel lsm = (ListSelectionModel) e.getSource();
                 * if (lsm.isSelectionEmpty()) { System.out.println("No rows are
                 * selected."); } else { int selectedRow =
                 * lsm.getMinSelectionIndex(); System.out.println("Row " +
                 * selectedRow + " is now selected."); _selectedRowIdx =
                 * selectedRow; }
                 */
            }
        });

        JScrollPane scroll = new JScrollPane(_tblColumns);
        _centerPanel.add(scroll, BorderLayout.CENTER);
    }

    private void initializeRightPanel()
    {

        _btnSaveData = new JButton("Save table");
        _btnAddSinhVien = new JButton("Add Column");
        _btnAddReferenceKey = new JButton("Set Reference Key");

        _btnDeleteSinhVien = new JButton("Delete Column");
        _btnUpdateSinhVien = new JButton("Update Column");


        _btnSaveData.setHorizontalAlignment(SwingConstants.LEFT);
        _btnAddSinhVien.setHorizontalAlignment(SwingConstants.LEFT);
        _btnDeleteSinhVien.setHorizontalAlignment(SwingConstants.LEFT);
        _btnUpdateSinhVien.setHorizontalAlignment(SwingConstants.LEFT);
        _btnAddReferenceKey.setHorizontalAlignment(SwingConstants.LEFT);


        _btnSaveData.setPreferredSize(new Dimension(150, 30));
        _btnAddSinhVien.setPreferredSize(new Dimension(150, 30));
        _btnDeleteSinhVien.setPreferredSize(new Dimension(150, 30));
        _btnUpdateSinhVien.setPreferredSize(new Dimension(150, 30));
        _btnAddReferenceKey.setPreferredSize(new Dimension(150, 30));

        _gridConstraints = new GridBagConstraints();
        _gridConstraints.anchor = GridBagConstraints.CENTER;
        _gridConstraints.fill = GridBagConstraints.NONE;
        _gridConstraints.insets = new Insets(12, 12, 12, 12);



        addComponent(_btnSaveData, 0, 0, 1, 1);

        addComponent(_btnAddSinhVien, 1, 0, 1, 1);
        addComponent(_btnAddReferenceKey, 2, 0, 1, 1);

        addComponent(_btnDeleteSinhVien, 3, 0, 1, 1);

        addComponent(_btnUpdateSinhVien, 4, 0, 1, 1);
    }

    private void addComponent(Component cpn, int row, int col, int widthInCell, int heightInCell)
    {
        _gridConstraints.gridx = col;
        _gridConstraints.gridy = row;
        _gridConstraints.gridwidth = widthInCell;
        _gridConstraints.gridheight = heightInCell;
        _rightPanel.add(cpn, _gridConstraints);
    }
}
